package struts.action.entreprise;

import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import metier.Entreprise;
import metier.Stage;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import struts.actionForm.entreprise.BeanSubmitStage;

import dao.DAOStage;


public class ActionSubmitStage extends Action 
{
	private DAOStage daos=null;
	
	public DAOStage getDaos() 
	{
		return daos;
	}
	
	public void setDaos(DAOStage daos) 
	{
		this.daos = daos;
	}
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)	throws Exception 
	{
		request.getSession().setAttribute("message_stage", null);
		try 
		{
			//recuperation des infos saisies par l'utilisateur
			BeanSubmitStage beanSubmitStage=new BeanSubmitStage();
			beanSubmitStage=(BeanSubmitStage) form;
			//transformations et tests des dates saisies
			SimpleDateFormat parseur= new SimpleDateFormat("dd/MM/yyyy");
			java.util.Date du,fu;
			du=parseur.parse(beanSubmitStage.getDebut());
			fu=parseur.parse(beanSubmitStage.getFin());
			Date debut,fin;
			debut= new Date(du.getTime());
			fin=new Date(fu.getTime());
			if(!debut.before(fin))
			{
				throw new Exception();
			}
			Entreprise entreprise=(Entreprise) request.getSession().getAttribute("ENTREPRISE");
			Stage stage = new Stage(entreprise, beanSubmitStage.getDepartement(), beanSubmitStage.getIntitule(), beanSubmitStage.getDescription(), beanSubmitStage.getLieu(), debut, fin);
			//sauvegarde de la proposition dans la BDD
			daos.save(stage);	
		} catch (Exception e) 
		{
			request.getSession().setAttribute("message_stage","ProposerStage.erreurs.submitestage");
			//e.printStackTrace();
			return mapping.findForward("SubmitStage");
		}
		request.getSession().setAttribute("message_stage", "ProposerStage.stagevalide");
		return mapping.findForward("SubmitStage");
	}

}
